﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.DBAccess;

public static class PatientRepository
{
    /// <summary>
    /// View dữ liệu trong table Patient
    /// </summary>
    public static DataSet LoadPatient(int companyId, bool isAdmin)
    {
        DBAccessor db = null;
        DataSet ds = null;
        try
        {
            //string sql = "SELECT * FROM Patient";
            string sql = "SELECT Code, PatientName, Pass, CompanyName FROM Patient, Company";
            if (companyId > 0)
            {
                sql = "SELECT Code, PatientName, Pass, Patient.FolderPath, Patient.CreatedDate FROM Patient, Company";
                sql += string.Format(" where Patient.CompanyID = Company.ID and CompanyId = {0}", companyId);

                if (!isAdmin)
                {
                    sql += " and CompanyName <> 'admin'";
                }
            }
            else
            {
                sql = "SELECT Code, PatientName, Pass, FolderPath, CreatedDate FROM Patient where CompanyId is null";
            }

            sql += " order by Code asc";

            db = new DBAccessor();
            ds = db.ExecuteQueryString(sql);

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (db != null) { db.Dispose(); }
        }
        return ds;
    }

    /// <summary>
    /// Import personal: bao gồm khách hàng cá nhân và khách hàng của cty
    /// </summary>
    /// <param name="code"></param>
    /// <param name="patientName"></param>
    /// <param name="companyId"></param>
    /// <param name="folderPath"></param>
    /// <returns></returns>
    public static int ImportPersonal(string code, string patientName, int companyId, string folderPath)
    {
        DBAccessor db = null;
        int id = 0;
        try
        {

            db = new DBAccessor();
            string sql = "SELECT * FROM Patient where FolderPath = '" + folderPath + "'";
            DataSet ds = db.ExecuteQueryString(sql);
            if (ds != null)
            {
                string insertSql = "";
                if (companyId > 0)
                    insertSql = string.Format("insert into Patient (Code, PatientName, CompanyID, Pass, FolderPath, CreatedDate) values (N\'{0}\',N\'{1}\',{2},N\'{3}\',N\'{4}\',N\'{5}\')", code, patientName, companyId.ToString(), FolderLib.FileProcessor.RandomString(5), folderPath, DateTime.Now);
                else
                    insertSql = string.Format("insert into Patient (Code, PatientName, Pass, FolderPath, CreatedDate) values (N\'{0}\',N\'{1}\',N\'{2}\',N\'{3}\',N\'{4}\')", code, patientName, FolderLib.FileProcessor.RandomString(5), folderPath, DateTime.Now);
                id = db.ExecuteNonQueryString(insertSql);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (db != null) { db.Dispose(); }
        }
        return id;
    }

    
}
